<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: SimpleTextPortlet.php 413 2011-01-04 21:22:15Z steen.rabol $
 * @package Pradoportal.Porttlets.SimpleTextPortlet.Edit
 *
 */

Prado::using('Application.Portlets.Portlet');
Prado::using('Application.Portlets.SimpleTextPortlet.Common.SimpleTextPortletRecord');

class SimpleTextPortlet extends Portlet
{
	private $_portletRecord	= null;

	public function  onInit($p)
	{
		parent::onInit($p);
		$this->_portletRecord = SimpleTextPortletRecord::finder()->findByPk($this->Parameters['id']);
	}

	public function onLoad($p)
	{
		parent::onLoad($p);
		if($this->_portletRecord)
		{
			$this->SimpleText->Text 	= $this->_portletRecord->content;
			$this->SimpleTextCSS->Text	= $this->_portletRecord->css;
		}
		else
		{
			$this->SimpleText->Text 	= '';
			$this->SimpleTextCSS->Text	= 'simpletextportlet';
		}
	}

	public function SaveContent($sender,$param)
	{

		if(!$this->_portletRecord)
		{
			$this->_portletRecord			= new SimpleTextPortletRecord();
			$this->_portletRecord->id		= (isset($this->Parameters['id']) ? $this->Parameters['id'] : 0);
		}

		$this->_portletRecord->content	= $this->SimpleText->Text;
		$this->_portletRecord->css		= $this->SimpleTextCSS->Text;
		$this->_portletRecord->culture	= $this->Application->Culture;
		$this->_portletRecord->save();

		$this->Page->gotoPage($this->Parameters['pagename'],array("PageMode" => $this->Page->EditMode));
	}

	public function CancelContent($sender,$param)
	{

		if($this->_portletRecord)
		{
			$this->SimpleText->Text	= $this->_portletRecord->content;
			$this->SimpleTextCSS->Text	= $this->_portletRecord->css;
		}
		else
		{
			$this->SimpleText->Text	= "";
			$this->SimpleTextCSS->Text	= "simpletextportlet";
		}
	}

	public function DeleteButtonClicked($sender,$param)
	{
		// Delete content from db
		$stRecord	= SimpleTextPortletRecord::finder()->findByPk($this->Parameters['id']);
		if($stRecord)
		{
			$stRecord->delete();
		}

		parent::DeleteButtonClicked($sender,$param);
	}
}
?>